package com.example.lenovo.a203_yhy_ordering_3.model;

import com.example.lenovo.a203_yhy_ordering_3.beans.IsCollectBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.UserCollectFoodBean;
import com.example.lenovo.a203_yhy_ordering_3.iface.UserCollectFoodIface;
import com.example.lenovo.a203_yhy_ordering_3.listener.UserCollectFoodListener;
import com.example.lenovo.a203_yhy_ordering_3.service.IsCollectService;
import com.example.lenovo.a203_yhy_ordering_3.service.UserCollectFoodService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lenovo on 2018/12/3.
 */

public class UserCollectFoodModel implements UserCollectFoodIface{
    private Retrofit retrofit;

    public UserCollectFoodModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void getUserCollectFoodResult(int user_id, int food_id, final UserCollectFoodListener userCollectFoodListener) {
        UserCollectFoodService userCollectFoodService = retrofit.create(UserCollectFoodService.class);

        Call<UserCollectFoodBean> userCollectFoodCall = userCollectFoodService.userCollectFood(user_id,food_id);

        userCollectFoodCall.enqueue(new Callback<UserCollectFoodBean>() {
            @Override
            public void onResponse(Call<UserCollectFoodBean> call, Response<UserCollectFoodBean> response) {
                if(response!=null)
                    userCollectFoodListener.onResponse(response.body());
                else
                    userCollectFoodListener.onFail("获取食品收藏失败");
            }

            @Override
            public void onFailure(Call<UserCollectFoodBean> call, Throwable t) {
                userCollectFoodListener.onFail(t.toString());
            }
        });
    }
}
